package com.fancon.android.emoji.keyboard;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RadioButton;

import com.fancon.android.emoji.Emoji;
import com.fancon.android.emoji.EmojiDockPanel.EmojiDockListener;
import com.fancon.android.emoji.EmojiKeyBoard.EmojiKeyBoardListener;
import com.fancon.android.emoji.EmojiUtil;
import com.fancon.android.emoji.R;
import com.fancon.android.emoji.widget.EmojiRadioButton;
import com.fancon.android.emoji.widget.M05RichText;

public class M05Panel extends BetterPopupWindow implements
		OnClickListener {
	private EmojiDockListener listener;
	private EmojiRadioButton radioSmiley, radioBell, radioVehicle, radioFlower,
			radioNumber;
	protected M05RichText richEditText;

	public M05Panel(View anchor, M05RichText richEditText) {
		super(anchor);
		this.richEditText = richEditText;
		// TODO Auto-generated constructor stub
	}

	public M05Panel(View anchor, M05RichText richEditText,
			EmojiDockListener listener) {
		super(anchor);
		// TODO Auto-generated constructor stub
		this.richEditText = richEditText;
		this.listener = listener;
	}

	protected void onCreate() {
		// inflate layout
		LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.emoji_panel,
				null);
		radioSmiley = (EmojiRadioButton) root.findViewById(R.id.radioSmiley);
		radioBell = (EmojiRadioButton) root.findViewById(R.id.radioBell);
		radioVehicle = (EmojiRadioButton) root.findViewById(R.id.radioVehicle);
		radioFlower = (EmojiRadioButton) root.findViewById(R.id.radioFlower);
		radioNumber = (EmojiRadioButton) root.findViewById(R.id.radioNumber);

		radioSmiley.setOnClickListener(this);
		radioBell.setOnClickListener(this);
		radioVehicle.setOnClickListener(this);
		radioFlower.setOnClickListener(this);
		radioNumber.setOnClickListener(this);

		this.setContentView(root);
	}

	public void showPanel(int keyboardHeight) {
		this.preShow();
		this.window.setFocusable(false);
		this.window.setTouchable(true);
		this.window.setOutsideTouchable(false);

		this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
		int[] location = new int[2];
		this.anchor.getLocationOnScreen(location);
		this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
		int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
		this.window.showAtLocation(this.anchor, Gravity.TOP, 0, screenHeight
				- keyboardHeight - this.root.getMeasuredHeight());
	}
	public void showFloatPanel(int keyboardHeight) {
		this.preShow();
		this.window.setFocusable(false);
		this.window.setTouchable(true);
		this.window.setOutsideTouchable(false);

		this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
		int[] location = new int[2];
		this.anchor.getLocationOnScreen(location);
		this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
		int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
		this.window.showAtLocation(this.anchor, Gravity.TOP, 0, screenHeight
				- keyboardHeight - this.root.getMeasuredHeight() - this.anchor.getMeasuredHeight());
	}
	public EmojiDockListener getDockListener() {
		return listener;
	}

	public void getDockListener(EmojiDockListener listener) {
		this.listener = listener;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v instanceof RadioButton) {
			if (v == radioSmiley) {
				openEmojiKeyBoard(EmojiUtil.SMILEY, v);
			} else if (v == radioBell) {
				openEmojiKeyBoard(EmojiUtil.BELL, v);
			} else if (v == radioVehicle) {
				openEmojiKeyBoard(EmojiUtil.VEHICLE, v);
			} else if (v == radioFlower) {
				openEmojiKeyBoard(EmojiUtil.FLOWER, v);
			} else if (v == radioNumber) {
				openEmojiKeyBoard(EmojiUtil.NUMBER, v);
			}
			if (listener != null) {
				listener.onItemSelected(v);
			}
		}
	}

	public void openEmojiKeyBoard(int category, View v) {
		final EmojiKeyBoardListener listener = new EmojiKeyBoardListener() {
			@Override
			public void onEmojiSelected(View view) {
				// TODO Auto-generated method stub
				String code = ((Emoji) view).getCode();
				Spanned cs = Html.fromHtml("<img src='" + code + "'/>",
						richEditText.getmEmojiUtil().getImageGetter(), null);
				int start = richEditText.getSelectionStart();
				EmojiUtil.insertSpannedTextToCurrentPosition(cs, richEditText,
						start, start);
			}

			@Override
			public void afterClose(View v) {
				// TODO Auto-generated method stub
			}
		};
		richEditText.hideKeyBoard();
		dismiss();
		M05KeyBoard dw = new M05KeyBoard(this.anchor, false, category, (EmojiKeyBoardListener) listener);
		dw.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				showKeyBoard();
			}
		});
		dw.showFloatKeyBoard();
	}
	public void showKeyBoard(){
		//InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		// only will trigger it if no physical keyboard is open
		//mgr.showSoftInput(RichEditText.this, InputMethodManager.SHOW_IMPLICIT);
		//Log.d("show keyboard", "show keyboard");
		InputMethodManager inputMgr = (InputMethodManager)this.anchor.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMgr.toggleSoftInput(0, 0);

	}

}
